/*eslint no-console: ["error", { allow: ["warn", "error", "log"] }] */

const {execFile} = require('child_process')


exports.submitCode = function submitCode(url, code) {
    return new Promise((resolve, reject) => {
        execFile('phantomjs', ['capture_sms.js', url, code], (error, stdout, stderr) => {
            if (error) {
                return reject(error)
            }
            if (stderr) {
                return reject(stderr)
            }
            if (stdout !== '') {
                return reject(stdout)
            }
            resolve()
        })
    })
}

exports.getCode = function getCode(url) {
    return new Promise((resolve, reject) => {
        execFile('phantomjs', ['capture_sms.js', url], (error, stdout, stderr) => {
            if (error) {
                return reject(error)
            }

            if (stderr) {
                return reject(stderr)
            }

            if (stdout !== '') {
                return reject(stdout)
            }

            process.stdout.write('请输入验证码：')
            process.stdin.on('data', function (chunk) {
                const code = (String(chunk))
                if (code !== '') {
                    process.stdin.end()
                    resolve(code)
                }
            })
        })
    })
}

/*
const url = 'https://passport.taobao.com/iv/mini/identity_verify.htm?tag=8&htoken=1fiHjpqwa7uUNXFSAOaB-sA9cA44fuE2TWxjEsB5JzPSgvVGHSnMzElrgyQToM4Q&appName='
getCode(url).then((code) => {
    return submitCode(url, code)
}).catch(error => {
    console.log(error)
})
*/



